/*
 * @Description  : 
 * @Version      : 1.0
 * @Author       : Shiming
 * @Date         : 2022-04-28 20:27:22
 * @LastEditors  : Shiming
 * @LastEditTime : 2022-05-11 16:04:16
 * @FilePath     : \\tms-obc-web\\src\\app\\routes\\partner\\level-config\\components\\edit\\edit.component.ts
 * Copyright (C) 2022 huzhenhong. All rights reserved.
 */
import { Component,  OnInit, ViewChild } from '@angular/core';
import { SFComponent, SFSchema,  SFTextareaWidgetSchema, SFUISchema } from '@delon/form';
import { _HttpClient } from '@delon/theme';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { ChannelSalesService } from '../../services/level-config.service';

@Component({
  selector: 'app-parter-LevelConfig-edit',
  templateUrl: './edit.component.html'
})
export class ParterLevelConfigEditComponent implements OnInit {
  @ViewChild('sf', { static: false }) sf!: SFComponent;
  schema!: SFSchema;
  ui!: SFUISchema;
  i: any;
  type: any;

  constructor(
    public service: ChannelSalesService,
    private modalRef: NzModalRef
  ) {}

  ngOnInit(): void {
    this.initSF();
  }
  initSF() {
    this.schema = {
      properties: {
        id: {
          type: 'string',
          title: '',
          ui: { hidden: true }
        },
        gradeName: {
          title: '等级名称',
          type: 'string',
          maxLength: 12
        },
        sortId: {
          title: '排序',
          minimum: 0,
          maximum: 999999,
          type:"number",
          ui: {
            change: (item: any) => {
             this.sf.setValue('/sortId', item?.toFixed(0))
            }
          }
        },
        remark: {
          type: 'string',
          title: '备注',
          maxLength: 50,
          ui: {
            widget: 'textarea',
            autosize: { minRows: 3, maxRows: 6 },
            placeholder:'请输入50字符'
          } as SFTextareaWidgetSchema,
        },
      },
      required: ['gradeName', 'sortId', 'remark']
    };
    this.ui = {
      '*': {
        spanLabelFixed: 120,
        grid: { span: 24 }
      },
    };
  }

  close() {
    this.modalRef.destroy();
  }
  save() {
    this.sf.validator({ emitError: true });
    if(!this.sf.valid) return;
    this.service.request(this.service.$api_save, { ...this.sf?.value }).subscribe(res => {
      if (res) {
        this.service.msgSrv.success('保存成功！')
        this.modalRef.destroy(true);
      } else {
        this.service.msgSrv.error(res.msg);
      }
    });
  }
}
